package com.breezelin.freshmarket.adpaters;

/**
 * Created by Breeze Lin
 * 2016/9/26 16:34
 * breezesummerlin@163.com
 */

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

import com.breezelin.fmlib.utils.FMLog;
import com.breezelin.freshmarket.R;

import java.util.List;

/**
 * 搜索热词的适配器
 */
public class HotWordAdapter extends BaseAdapter {

	/**
	 * 上下文
	 */
	private Context context;
	/**
	 * 热词数据
	 */
	private List<String> contents;
	/**
	 * 列表监听
	 */
	private HotWordListener listener;

	/**
	 * 搜索热词适配器
	 *
	 * @param context  上下文
	 * @param contents 热词内容
	 * @param listener 监听
	 */
	public HotWordAdapter(Context context, List<String> contents, HotWordListener listener) {
		this.context = context;
		this.contents = contents;
		this.listener = listener;
	}

	@Override
	public int getCount() {
		int count = 0;
		if (contents != null) {
			count = contents.size();
		}
		return count;
	}

	@Override
	public Object getItem(int position) {
		return contents.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		View ret = convertView != null ? convertView : LayoutInflater.from(context)
				.inflate(R.layout.item_hot_word, parent, false);
		String content = contents.get(position);
		TextView txt = (TextView) ret.findViewById(R.id.btn);
		// 条目绑定搜索热词
		txt.setTag(content);
		// 设置条目视图
		txt.setText(content);
		// 为新创建的条目添加监听
		if (convertView == null) {
			txt.setOnClickListener(new View.OnClickListener() {
				@Override
				public void onClick(View v) {
					FMLog.d("HotWordAdapter", this.toString());
					// 条目被点击，则将当前条目绑定的热词回调给监听
					String content = (String) v.getTag();
					listener.onHotWordClick(content);
				}
			});
		}
		return ret;
	}

	/**
	 * 热词列表监听
	 */
	public interface HotWordListener {

		/**
		 * 热词点击回调
		 *
		 * @param hotWord 热词内容
		 */
		void onHotWordClick(String hotWord);
	}
}
